function checkLoginId()
{
	var loginId = $("loginId");		
	if(!sl_check(loginId,"用户名",50,3)) return false;
	if(!sl_checkEn(loginId,"用户名"))  return false;
	buffalo.remoteCall("userServiceAjaxService.validateLoginId",[loginId.value], function(reply) {
        var ret = reply.getResult();
        if(ret!=null)
        {
        	sl_alert(ret);
        	loginId.focus();
			loginId.select();
        }
        else
        {
        	sl_alert("该用户名可用！");
        }
    });
}

function checkEmail(userId,email)
{
	if (email.value.length > 0 && !(/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/g.test(email.value))) {
		$("checkEmail").innerText = "Email格式错误，请重新输入！";
		email.focus();
		email.select();
		return false;
	}
	buffalo.remoteCall("userServiceAjaxService.validateEmail",[userId,email.value], function(reply) {
        var ret = reply.getResult();
        if(ret!=null)
        {
	        $("checkEmail").innerText = ret;
	        email.focus();
			email.select();			
		}
		else
		{
			$("checkEmail").innerText = "";
		}
    });
}


function showExtra(obj)
{	
	if(obj.checked)
	{		
		$("extraProp").style.display = "";		
	}
	else
	{	
		$("extraProp").style.display = "none";	
	}
}

function changeAreaName(obj)
{	
	$("area").value = obj.options[obj.selectedIndex].text;
	
	if(obj.selectedIndex>0)
	{
		var areaId = parseInt(obj.value);		
		buffalo.remoteCall("areaServiceAjaxService.getSubCitiesOptions",[areaId], function(reply) {
	        var cities = reply.getResult();
		        var cityArry = cities.split(",");	       
			    var city = $("cities");
			    $("cityOptions").style.display="";
			    city.options.length = cityArry.length;		   
			    for(i=0;i<cityArry.length-1;i++)
			    {		     
			      	city.options[i+1].text = cityArry[i];		        	
			    }
		    
	    });
    }
    else
    {
    	$("cityOptions").style.display="none";
    }
}

function changeCity(obj)
{
	if(obj.selectedIndex>0)
		$("city").value = obj.options[obj.selectedIndex].text;
	else
		$("city").value = "";
}

function doSubmit() {

	form = document.theform;
	
	if(!validateForm(form))
		return;

	var obj = {};
	obj[Buffalo.BOCLASS] = "java.util.HashMap";
	obj.loginId=$F('loginId');
	obj.password=$F('password');
	obj.userName=$F('userName');
	obj.sex=getRadioValue('sex');
	obj.email=$F('email');
	obj.emailOpenFlag=$F('emailOpenFlag');
	obj.passQuestion=$F('passQuestion');
	obj.passAnswer=$F('passAnswer');
	obj.personalSign = $F('personalSign').trimHTML();
	obj.personalIntro = $F('personalIntro');
	obj.regIp=opIp;
	
	var saveDetailFlag = form.checkExtra.checked;	
	var obj2 = {};
	obj2[Buffalo.BOCLASS] = "java.util.HashMap";
	if(saveDetailFlag)
	{
		obj2.trueName=$F('trueName');
		obj2.birthday=$F('birthday');
		obj2.mobile=$F('mobile');
		obj2.telephone=$F('telephone');
		obj2.address=$F('address');
		obj2.postNo=$F('postNo');
		obj2.qq=$F('qq');
		obj2.msn=$F('msn');
		obj2.personalWeb=$F('personalWeb');
		obj2.area=$F('area');
		obj2.city=$F('city');
		obj2.job=$F('job');
	}
    buffalo.remoteCall("userServiceAjaxService.doUserReg",[obj,obj2,saveDetailFlag], function(reply) {
        ajaxSubmit(reply.getResult(),"注册成功！","back");
    });	
		
}	

function doUpdateSubmit() {

	form = document.theform;
	
	if(!validateUpdateForm(form))
		return;

	var obj = {};
	obj[Buffalo.BOCLASS] = "java.util.HashMap";
	
	obj.userId = userId;
	//obj.loginId=$F('loginId');
	obj.password=$F('password');
	obj.userName=$F('userName');
	obj.sex=getRadioValue('sex');
	obj.email=$F('email');
	obj.emailOpenFlag=$F('emailOpenFlag');
	//obj.passQuestion=$F('passQuestion');
	//obj.passAnswer=$F('passAnswer');	
	obj.personalSign = FCKeditorAPI.GetInstance('personalSign').GetXHTML(true);
	obj.personalIntro=$F('personalIntro');
	//obj.regIp=opIp;
	
	var saveDetailFlag = form.checkExtra.checked;	
	var obj2 = {};
	obj2[Buffalo.BOCLASS] = "java.util.HashMap";
	if(saveDetailFlag)
	{
		obj2.trueName=$F('trueName');
		obj2.birthday=$F('birthday');
		obj2.mobile=$F('mobile');
		obj2.telephone=$F('telephone');
		obj2.address=$F('address');
		obj2.postNo=$F('postNo');
		obj2.qq=$F('qq');
		obj2.msn=$F('msn');
		obj2.personalWeb=$F('personalWeb');
		obj2.area=$F('area');
		obj2.city=$F('city');
		obj2.job=$F('job');
	}
    buffalo.remoteCall("userServiceAjaxService.updateUser",[obj,obj2,saveDetailFlag], function(reply) {
        ajaxSubmit(reply.getResult(),"修改成功！");
    });	
}		


function validateForm(form){
	if (form.loginId.value.length == 0) {
		sl_alert("请输入您的用户名！");
		form.loginId.focus();
		return false;
	}
	if (!/^([a-zA-z_]{1})([\w]*)$/g.test(form.loginId.value)){
			sl_alert("'用户名'只可包含字母、数字或下划线，且开头的只能是字母或下划线！");
			form.loginId.focus();
			form.loginId.select();
			return false;
	}		
	if (form.loginId.value.length < 3) {
		sl_alert("用户名应大于3个小于50个字符！");
		form.loginId.focus();
		return false;
	}
	if (form.loginId.value.length > 50) {
		sl_alert("用户名应大于3个小于50个字符！");
		form.loginId.focus();
		return false;
	}	
	if (form.password.value.length == 0) {
		sl_alert("请输入您的密码！");
		form.password.focus();
		return false;
	}
		if (form.password.value.length < 6) {
		sl_alert("密码至少6位！");
		form.password.focus();
		return false;
	}
	if (form.password2.value.length == 0) {
		sl_alert("请确认您的密码！");
		form.password2.focus();
		return false;
	}
	if (form.password.value != form.password2.value) {
		sl_alert("您两次输入的密码不一样，请重新输入！");
		form.password.focus();
		return false;
	}
	if (form.email.value.length == 0) {
		sl_alert("请输入您的Email！");
		form.email.focus();
		return false;
	}
	if (form.email.value.length > 0 && !form.email.value.match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) ) {
		sl_alert("Email格式错误，请重新输入！");
		form.email.focus();
		return false;
	}
		if (form.personalSign.value.length > 500) {
		sl_alert("您的签名太长了，换短点吧！");
		form.personalSign.focus();
		return false;
	}
	if (form.personalIntro.value.length > 500) {
		sl_alert("自我介绍不能超过500字符！");
		form.personalIntro.focus();
		return false;
	}
	
	return true;
}


function validateUpdateForm(form){

	if(form.password.value!="")
	{
		if (form.password.value.length == 0) {
			sl_alert("请输入您的密码！");
			form.password.focus();
			return false;
		}
			if (form.password.value.length < 6) {
			sl_alert("密码至少6位！");
			form.password.focus();
			return false;
		}
		if (form.password2.value.length == 0) {
			sl_alert("请确认您的密码！");
			form.password2.focus();
			return false;
		}
		if (form.password.value != form.password2.value) {
			sl_alert("您两次输入的密码不一样，请重新输入！");
			form.password.focus();
			return false;
		}
	}	
	if (form.email.value.length == 0) {
		sl_alert("请输入您的Email！");
		form.email.focus();
		return false;
	}
	if (form.email.value.length > 0 && !form.email.value.match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) ) {
		sl_alert("Email格式错误，请重新输入！");
		form.email.focus();
		return false;
	}
		if (form.personalSign.value.length > 500) {
		sl_alert("您的签名太长了，换短点吧！");
		form.personalSign.focus();
		return false;
	}
	if (form.personalIntro.value.length > 500) {
		sl_alert("自我介绍不能超过500字符！");
		form.personalIntro.focus();
		return false;
	}
	
	return true;
}

var ratingMsgs = new Array(6);
var ratingMsgColors = new Array(6);
var barColors = new Array(6);
ratingMsgs[0] = "未评级";
ratingMsgs[1] = "太短";
ratingMsgs[2] = "弱";
ratingMsgs[3] = "一般";
ratingMsgs[4] = "很好";
ratingMsgs[5] = "极佳"; //If the password server is down
ratingMsgColors[0] = "#808080";
ratingMsgColors[1] = "#da5301";
ratingMsgColors[2] = "#ccbe00";
ratingMsgColors[3] = "#1e91ce";
ratingMsgColors[4] = "#0000FF";
ratingMsgColors[5] = "#ff0000";
barColors[0] = "#e0e0e0";
barColors[1] = "#da5301";
barColors[2] = "#f0e54b";
barColors[3] = "#1e91ce";
barColors[4] = "#0000FF";
barColors[5] = "#ff0000";

function rateUserPass(obj)
{
	var password = obj.value;
	if(password.length==0)
	{
		DrawBar(0);
	}
	else if(password.length<6)  //密码必需大于6位
	{
		DrawBar(1);
	}
	else
	{
	  var lv = 1;	  
	  if (password.match(/[a-z]/ig)){lv++;}
 	  if (password.match(/[0-9]/ig)){lv++;} 	 
 	  if (password.match(/(.[^a-z0-9])/ig)){lv++;}
 	  if (password.length < 6 && lv > 0){lv--;}
 	  if (password.length > 15 && lv > 0){lv++;}
	  switch(lv) 
	  { 
		 case 0:
			 DrawBar(0);
			 break;
		 case 1:
			 DrawBar(1);
			 break;
		 case 2:
			 DrawBar(2);
			 break;
		 case 3:
			 DrawBar(3);
			 break;
		 case 4:
			 DrawBar(4);
			 break;
		 case 5:
			 DrawBar(5);
			 break;
		 default:
		 	DrawBar(0);
 	  }	  
	  // 	buffalo.remoteCall("userServiceAjaxService.save",[password], function(reply) {
      // 	var rating = parseInt(reply.getResult());
      // 	DrawBar(rating);
      //});	
	}
	
	
}

function DrawBar(rating) 
{
    var posbar = $('posBar');
    var negbar = $('negBar');
    var passwdRating = $('passwdRating');
    var barLength = $('passwdBarDiv').width;
    if (rating >= 0 && rating <= 5) 
    {  //We successfully got a rating
      posbar.style.width = barLength / 5 * rating;
      negbar.style.width = barLength / 5 * (5 - rating);
    } else 
    {
      posbar.style.width = 0;
      negbar.style.width = barLength;
      rating = 5; // Not rated Rating
    }
    posbar.style.background = barColors[rating]
    passwdRating.innerHTML = "<font color='" + ratingMsgColors[rating] +
                             "'>" + ratingMsgs[rating] + "</font>";
}
  
